﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace ST.Dal
{
    /// <summary>
    /// 抽象数据库底层基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class AbstractDatabase<T> where T:class
    {
       
        public abstract string TableName { get; }
        public abstract string PrimaryKey { get; }
        public abstract string Fields { get; }
        public abstract T PopulateObject(IDataReader reader);
        private Database _database;
        public Database CreateDatebase()
        {
            if (_database == null)
                _database = new SqlDatabase("");
            return _database;
        }
        public virtual T GetObject(int objectID)
        {
            if (objectID <= 0)
                return null;
            string getObjectSql = string.Format("select {0} from {1} where {2}=@ID",Fields,TableName,PrimaryKey);
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(getObjectSql);
            db.AddInParameter(command, "@ID",DbType.Int32,objectID);
            IDataReader reader = db.ExecuteReader(command);
            return GetObjectFromReader(reader);
        }
        protected T GetObjectFromReader(IDataReader reader)
        {
            using (reader)
            {
                if (reader.Read())
                    return PopulateObject(reader);
                else
                    return null;
            }
        }
        /// <summary>
        /// 通过主键删除对象
        /// </summary>
        /// <param name="objectID"></param>
        /// <returns></returns>
        public bool DeleteObject(int objectID)
        {
            if (objectID <= 0)
                return false;                
            Database db = CreateDatebase();
            string delSql = string.Format("delete from {0} where {1}=@ID",TableName,PrimaryKey);
            DbCommand command = db.GetSqlStringCommand(delSql);
            db.AddInParameter(command, "@ID", DbType.Int32, objectID);
            return db.ExecuteNonQuery(command) > 0;
        }
        /// <summary>
        /// 获取最新的几条数据记录
        /// </summary>
        /// <param name="where">不需要加where</param>
        /// <param name="TopSize"></param>
        /// <returns></returns>
        public List<T> GetList(string where, int TopSize)
        {
            string condition = " 1=1 ";
            if (!string.IsNullOrEmpty(where))
                condition += " and "+where;
            
            string getSql = string.Format("select {0} from {1} where {2}",Fields,TableName,where);
            if (TopSize > 0)
                getSql = string.Format("select top {0} {1} from {2} where {3}",TopSize, Fields, TableName, where);
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(getSql);
            IDataReader reader = command.ExecuteReader();
            T t = GetObjectFromReader(reader);
            List<T> list = new List<T>();
            if (t != null)
                list.Add(t);
            return list;
           
        }

        protected string BuildInsertString(params string[] Fields)
        {
            string[] pars = new string[Fields.Length];
            for (int i = 0; i < Fields.Length; i++)
            {
                pars[i] = string.Concat("@", Fields[i]);
            }
            return string.Format("insert into {0} ({1})values({2}); SELECT SCOPE_IDENTITY();", TableName, string.Join(",", Fields), string.Join(",", pars));
 
        }
        protected string BuildUpdateString(params string[] Fields)
        {
            string[] pars = new string[Fields.Length];
            for (int i = 0; i < Fields.Length; i++)
            {
                pars[i] = string.Concat(Fields[i], "=","@", Fields[i]);
            }
            return string.Format("update {0} set {1} where {2}={3}", TableName,string.Join(",",pars),PrimaryKey,"@"+PrimaryKey);
        }

    }
}
